package com.douqu.game.core.entity.ext.data.major;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.config.MailConfig;
import com.douqu.game.core.config.StableDataConfig;
import com.douqu.game.core.config.common.CommonData;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.config.sprite.MasterConfig;
import com.douqu.game.core.config.talent.TalentConfig;
import com.douqu.game.core.config.talent.TalentLevelConfig;
import com.douqu.game.core.config.talent.TalentRefineConfig;
import com.douqu.game.core.config.talent.TalentUpConfig;
import com.douqu.game.core.e.E_MailModelType;
import com.douqu.game.core.e.E_StableDataType;
import com.douqu.game.core.e.E_TalentTabType;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.db.MailDB;
import com.douqu.game.core.entity.ext.BagInfo;
import com.douqu.game.core.entity.ext.MailInfo;
import com.douqu.game.core.entity.ext.data.BaseData;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.TalentFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by dengbo on 2018/10/15 0015.
 */
public class TalentData  extends BaseData {

    //类型{天赋所属页签}- 玩家习得天赋
    private Map<Integer, List<TalentBean>> talentLibraryMap = new ConcurrentHashMap<>();

    //玩家已使用的天赋点 类型 强化or精炼消耗 天赋点
    private Map<Integer,Map<Integer,TalentUsedBean>> usedTalentNoteMap = new ConcurrentHashMap<>();

    //天赋经验值
    private int talentExp;

    //天赋等级
    private int talentLv;

    //玩家拥有天赋点
    private int talentNote;

    private Player player;

    //对玩家经验值做矫正，防止配置改动导致等级升高或者降低
    private Boolean isTalentExp = false;

    public TalentData(Player player){
        this.player = player;
    }

    @Override
    public void init() {
        initTalent();
    }

    @Override
    public void checkInit() {
        if (talentLv == 0){
            init();
        }
    }

    @Override
    public void reset() {
    }

    @Override
    public void checkReset() {
    }

    @Override
    public void writeTo(ByteBuffer buffer)
    {
        buffer.writeByte(talentLibraryMap.size());
        for(Map.Entry<Integer, List<TalentBean>> entry : talentLibraryMap.entrySet())
        {
            buffer.writeByte(entry.getKey());//天赋类型
            buffer.writeShort(entry.getValue().size());
            for (TalentBean bean:entry.getValue()){
                buffer.writeInt(bean.getTalentId());
                buffer.writeInt(bean.getLevel());
                buffer.writeInt(bean.getRefineLevel());
            }
        }
        buffer.writeByte(usedTalentNoteMap.size());
        for (Map.Entry<Integer,Map<Integer,TalentUsedBean>> entry:usedTalentNoteMap.entrySet())
        {
            buffer.writeByte(entry.getKey());
            buffer.writeByte(entry.getValue().size());
            for (Map.Entry<Integer,TalentUsedBean> entry1:entry.getValue().entrySet()){
                buffer.writeByte(entry1.getKey());
                buffer.writeInt(entry1.getValue().getUseCount());
                buffer.writeInt(entry1.getValue().getUseNote());
            }
        }
        buffer.writeInt(talentExp);
        buffer.writeInt(talentLv);
        buffer.writeInt(talentNote);
    }

    @Override
    public void loadFrom(ByteBuffer buffer)
    {
        int talentLibrarySize = buffer.readByte();
        talentLibraryMap.clear();
        List<TalentBean> dataList = null;
        for(int i = 0; i < talentLibrarySize; i++){
            int key = buffer.readByte();
            int listSize = buffer.readShort();
            dataList = new CopyOnWriteArrayList<>();
            for(int j = 0; j < listSize; j++){
                dataList.add(new TalentBean(buffer.readInt(), buffer.readInt(), buffer.readInt()));
            }
            talentLibraryMap.put(key, dataList);
        }
        int usedTalentNoteSize = buffer.readByte();
        usedTalentNoteMap.clear();
        Map<Integer,TalentUsedBean> map = null;
        for(int i = 0; i < usedTalentNoteSize; i++){
            int key = buffer.readByte();
            int mapSize = buffer.readByte();
            map = new ConcurrentHashMap<>();
            for (int j = 0; j < mapSize; j++){
                map.put(buffer.readByte(), new TalentUsedBean(buffer.readInt(), buffer.readInt()));
            }
            usedTalentNoteMap.put(key,map);
        }

        talentExp = buffer.readInt();
        talentLv = buffer.readInt();
        talentNote = buffer.readInt();
        return;
    }


    /**
     * 初始化天赋
     */
    public void initTalent(){
        this.talentLibraryMap = new HashMap<>();
        this.usedTalentNoteMap = new HashMap<>();
        this.talentExp = 0;
        this.talentLv = 1;
        //一级默认天赋点
        TalentLevelConfig config= DataFactory.getInstance().getGameObject(DataFactory.TALENT_LEVEL,1);
        this.talentNote = config.upReward;
    }

    /**
     * 添加天赋经验值
     * @param talentExp
     */
    public void addTalentExp(int talentExp){
        List<TalentLevelConfig> list= DataFactory.getInstance().getDataList(DataFactory.TALENT_LEVEL);
        TalentLevelConfig config;
        //校验经验值
        checkExp();
        //累加经验
        this.talentExp = this.talentExp + talentExp;
        //获取下一等级所需经验值
        MailConfig mailConfig;
        MailDB mailDB;
        for (int i = talentLv;i<list.size();i++){
            config = list.get(i);
            if (config.upTalent > this.talentExp){
               break;
            }
            this.talentLv = i + 1;
            //发送邮件通知
            mailConfig = DataFactory.getInstance().getGameObject(DataFactory.MAIL_MODEL_KEY, E_MailModelType.TALENT_LV_UP.getCode());
            mailDB = new MailDB(mailConfig.title, mailConfig.content,null);
            player.getExtInfo(MailInfo.class).addMail(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_SYSTEM_VALUE, mailDB);
            // 添加天赋点
            talentNote = talentNote+ config.upReward;
        }
    }


    /**
     * 清空某一页的天赋
     * @param type 类型
     */
    public boolean clearTalent(SGCommonProto.E_TALENT_TYPE type){
        //initTalent();
       List<TalentBean> beanList = talentLibraryMap.get(type.getNumber());
        Map<Integer,TalentUsedBean> usedBeanMap= usedTalentNoteMap.get(type.getNumber());
        if (beanList == null ||beanList.size() == 0 ||usedBeanMap == null){
            LogUtils.info("天赋为空 ->"+type);
            return false;
        }
        BagInfo bagInfo = player.getExtInfo(BagInfo.class);
        // 消耗钻石  多少级免费不用消耗钻石
        StableDataConfig dataConfig = DataFactory.getInstance().getGameObject(DataFactory.STABLE_DATA_KEY, E_StableDataType.TALENT_CLEAR_LEVEL.getCode());
        if (player.getLv() >dataConfig.intValue){
            switch (type){
                case TALENT_TYPE_ATK:
                    dataConfig = DataFactory.getInstance().getGameObject(DataFactory.STABLE_DATA_KEY, E_StableDataType.TALENT_ATK_CLEAR.getCode());
                    break;
                case TALENT_TYPE_DEF:
                    dataConfig = DataFactory.getInstance().getGameObject(DataFactory.STABLE_DATA_KEY, E_StableDataType.TALENT_DEF_CLEAR.getCode());
                    break;
                case TALENT_TYPE_STRATEGY:
                    dataConfig = DataFactory.getInstance().getGameObject(DataFactory.STABLE_DATA_KEY, E_StableDataType.TALENT_SLG_CLEAR.getCode());
                    break;
            }
            int goodsCount = bagInfo.getGoodsCount(dataConfig.goods[0].type, dataConfig.goods[0].id);
            if (goodsCount < dataConfig.goods[0].value) {
                LogUtils.debug("资源不够 自己拥有:" + goodsCount + "     ,需要：" + dataConfig.goods[0].value);
                return false;
            }
            //扣除钻石
            bagInfo.addGoods(new GoodsData[0], dataConfig.goods);

        }
        //返还道具
        TalentBean talentBean;
        TalentConfig talentConfig;
        List<TalentUpConfig> upConfigList;
        List<TalentRefineConfig> refineConfigList;
        Map<String,Integer> map = new HashMap<>();
        GoodsData[] goodsData;
        int goodsValue;
        String key;
        for (int i = 0 ;i<beanList.size(); i++){
            talentBean=  beanList.get(i);
            //取的已学习的技能相关的配置
            talentConfig =  DataFactory.getInstance().getGameObject(DataFactory.TALENT_BEAN,talentBean.getTalentId());
            if (talentConfig.type == type.getNumber()){
                //返还加点消耗
                upConfigList = DataFactory.getInstance().getTalentUpMap().get(talentBean.getTalentId());
                for (TalentUpConfig upConfig: upConfigList){
                    if (upConfig.level< talentBean.getLevel()){
                        goodsData =upConfig.restitution;
                        for (int j=0;j<goodsData.length;j++){
                            key =goodsData[j].type+";"+goodsData[j].id;
                            if (map.get(key) == null){
                                map.put(key,goodsData[j].value);
                                LogUtils.info("key="+key+"value:"+goodsData[j].value);
                            }else {
                                goodsValue =map.get(key) +goodsData[j].value;
                                map.put(key,goodsValue);
                            }
                        }
                    }
                }
                //返还精炼消耗
                refineConfigList =DataFactory.getInstance().getTalentRefineConfigMap().get(talentBean.getTalentId());
                for (TalentRefineConfig refineConfig: refineConfigList){
                    if (refineConfig.level< talentBean.getRefineLevel()){
                        goodsData = refineConfig.restitution;
                        for (int j=0;j<goodsData.length;j++){
                            key =goodsData[j].type+";"+goodsData[j].id;
                            if (map.get(key) == null){
                                map.put(key,goodsData[j].value);
                                LogUtils.info("key="+key+"value:"+goodsData[j].value);
                            }else {
                                goodsValue =map.get(key) +goodsData[j].value;
                                map.put(key,goodsValue);
                            }
                        }
                        //bagInfo.addGoods(refineConfig.restitution);
                    }
                }
            }
        }
        //道具合并
        goodsData = new GoodsData[map.size()];
        int index = 0;
        GoodsData goods;
        int[] value;
        for (Map.Entry<String,Integer> entry:map.entrySet()){
            String id = entry.getKey();
            value = LoadUtils.loadIntArray("clearTalent", id);
            goods = new GoodsData(value[0],value[1],entry.getValue());
            goodsData[index] = goods;
            index ++;
        }
        bagInfo.addGoods(goodsData);
        //返还天赋点
        int addNote = 0;
        for (TalentUsedBean bean:usedTalentNoteMap.get(type.getNumber()).values()){
            addNote += bean.useNote;
        }
        LogUtils.info("返还天赋点数："+usedBeanMap);
        talentNote = talentNote+ addNote;
        //清空该页已消耗的天赋点
        usedTalentNoteMap.remove(type.getNumber());
        //清空该类型的数据
        talentLibraryMap.remove(type.getNumber());

        return true;
    }

    /**
     * 加点精炼
     * @param tabType 加点or精炼
     * @param type  天赋类型
     * @param talentId 天赋id
     * @return
     */
    public boolean updateTalent(int tabType,int type,int talentId){
        LogUtils.info("-----------参数 tabTpye:"+tabType +"===type ="+type +"=id = "+talentId);
        BagInfo bagInfo = player.getExtInfo(BagInfo.class);
        TalentBean talentBean;
        Map<Integer,TalentUsedBean> map;
        TalentUsedBean usedBean ;
        if ( usedTalentNoteMap.get(type) == null){
            map = new HashMap<>();
            usedBean = new TalentUsedBean(0,0);
            map.put(tabType,usedBean);
            usedTalentNoteMap.put(type,map);
        }
        if (usedTalentNoteMap.get(type).get(tabType)== null){
            map =usedTalentNoteMap.get(type);
            usedBean = new TalentUsedBean(0,0);
            map.put(tabType,usedBean);
        }
        StableDataConfig dataConfig = DataFactory.getInstance().getGameObject(DataFactory.STABLE_DATA_KEY, E_StableDataType.TALENT_LEVEL_UP.getCode());

        switch (tabType){
            case TalentFactory.TALENT_ADD:
                List<TalentUpConfig> configList= DataFactory.getInstance().getTalentUpMap().get(talentId);
                //配置不存在
                if (configList == null){
                    LogUtils.info("TalentUpConfig配置数据为空 talentId:"+ talentId);
                    return false;
                }
                if (dataConfig.stringValue[0]<= usedTalentNoteMap.get(type).get(tabType).getUseCount()){
                    LogUtils.info("次数不足 :"+ usedTalentNoteMap.get(type).get(tabType).getUseCount());
                    return false;
                }
                //获得当前操作符文加点等级和精炼等级
                 talentBean = getTalentBean(type,talentId);
                //该符文还没操作
                if (talentBean == null){
                    talentBean = new TalentBean(talentId,0,0);
                    List<TalentBean> list  = talentLibraryMap.get(type);
                    if (list == null){
                        list = new ArrayList<>();
                        list.add(talentBean);
                        talentLibraryMap.put(type, list);

                    }else {
                        LogUtils.info("-----type=-" + type + "---------list =" + list.size());
                        for (TalentBean b : list) {
                            LogUtils.info("-----id=-" + b.getTalentId() + "---------lv =" + b.getLevel() + "---------lv2 =" + b.getRefineLevel());
                        }
                        list.add(talentBean);
                    }


                }
                TalentUpConfig talentUpConfig= null;
                for(TalentUpConfig config :configList){
                    //加点，获得当前等级升级消耗的配置
                    if (config.level == talentBean.getLevel() && tabType == E_TalentTabType.ADD.getCode()){
                        //不能超过最大等级
                        if (configList.size() >talentBean.getLevel()+1)
                        talentUpConfig = config;
                        break;
                    }

                }
                //得到下一级的配置
                if (talentUpConfig == null){
                    LogUtils.info("已满级或者未开放 talentId:"+ talentId +"当前等级：lv:"+ talentBean.getLevel());
                    return false;
                }
                //前置条件是否满足
                if (talentUpConfig.condition != 0 ){
                    if (talentUpConfig.condition >usedTalentNoteMap.get(type).get(tabType).getUseNote()){
                        LogUtils.info("消耗天赋点不够 talentId:"+ talentId +"note:"+
                                usedTalentNoteMap.get(type).get(tabType).getUseNote());
                        return false;
                    }
                }
                bagInfo = player.getExtInfo(BagInfo.class);
                //判断天赋点数量是否足够
                if ( talentUpConfig.upAsset > talentNote){
                    LogUtils.info("天赋点数不足 拥有的:"+ talentNote +"需要的："+talentUpConfig.upAsset);
                    return false;
                }
                //判断道具数量是否足够
                GoodsData[] needGoods= talentUpConfig.upCost;
                for (int i = 0; i<needGoods.length;i++){
                    if (bagInfo.getPropCount(needGoods[i].id)< needGoods[i].value){
                        LogUtils.info("道具-"+ needGoods[i].value+"数量不足，拥有："+
                                bagInfo.getPropCount(needGoods[i].id)+ "需要："+ needGoods[i].value);
                        return false;
                    }
                }
                //扣除道具
                bagInfo.subGoods(needGoods);
                //扣除天赋点
                talentNote =  talentNote - talentUpConfig.upAsset;
                //添加记录
                talentBean.setLevel(talentBean.getLevel() + 1);
                //添加已使用天赋点记录
                usedBean=  usedTalentNoteMap.get(type).get(tabType);
                usedBean.setUseCount(usedBean.getUseCount() + 1);
                usedBean.setUseNote(usedBean.getUseNote() + 1);
                return true;
            case TalentFactory.TALENT_REFINE:
                List<TalentRefineConfig> refineList= DataFactory.getInstance().getTalentRefineConfigMap().get(talentId);
                //配置不存在
                if (refineList == null){
                    LogUtils.info("TalentUpConfig配置数据为空 talentId:"+ talentId);
                    return false;
                }
                if (dataConfig.stringValue[1]<= usedTalentNoteMap.get(type).get(tabType).getUseCount()){
                    LogUtils.info("次数不足 :"+ usedTalentNoteMap.get(type).get(tabType).getUseCount());
                    return false;
                }
                //获得当前操作符文加点等级和精炼等级
                 talentBean = getTalentBean(type,talentId);
                //该符文还没操作
                if (talentBean == null){
                    LogUtils.info("=============该铭文没有记录 id="+talentId);
                    talentBean = new TalentBean(talentId,0,0);
                    List<TalentBean> list =talentLibraryMap.get(type);
                    if (list == null)
                        list = new ArrayList<>();
                    list.add(talentBean);
                    talentLibraryMap.put(type, list);
                }
                TalentRefineConfig talentFefineConfig= null;
                for(TalentRefineConfig config :refineList){
                    //精炼，获得当前等级下一级的配置
                    if (config.level == talentBean.getRefineLevel()  && tabType == E_TalentTabType.REFINE.getCode()){
                        //不能超过最大等级
                        if (refineList.size() >talentBean.getRefineLevel()+1)
                        talentFefineConfig = config;
                        break;
                    }
                }
                //得到下一级的配置
                if (talentFefineConfig == null){
                    LogUtils.info("已满级或者未开放 talentId:"+ talentId +"当前等级：lv:"+ talentBean.getLevel());
                    return false;
                }
                //判断前置条件
                CommonData[] refineCondition= talentFefineConfig.condition;
                TalentBean bean;
                for (int i = 0; i<refineCondition.length;i++){
                    bean = getPlayerTalent(refineCondition[i].id);
                    if (bean == null){
                        LogUtils.info("该天赋还未加点 talentId:"+ talentId);
                        return false;
                    }
                    if (bean.getLevel() < refineCondition[i].value){
                        LogUtils.info("该天赋加点等级不够 talentId:"+ talentId +"lv:"+bean.getLevel()
                                +"needLv:"+refineCondition[i].value);
                        return false;
                    }
                }
                //判断道具数量是否足够
                GoodsData[] refineCost= talentFefineConfig.refineCost;
                for (int i = 0; i<refineCost.length;i++){
                    if (bagInfo.getPropCount(refineCost[i].id)< refineCost[i].value){
                        LogUtils.info("道具-"+ refineCost[i].value+"数量不足，拥有："+
                                bagInfo.getPropCount(refineCost[i].id)+ "需要："+ refineCost[i].value);
                        return false;
                    }
                }
                //判断货币数量是否足够
                GoodsData[] refineCostAsset= talentFefineConfig.refineCostAsset;
                for (int i = 0; i<refineCostAsset.length;i++){
                    if (bagInfo.getAsset(refineCostAsset[i].id)< refineCostAsset[i].value){
                        LogUtils.info("道具-"+ refineCostAsset[i].value+"数量不足，拥有："+
                                bagInfo.getPropCount(refineCostAsset[i].id)+ "需要："+ refineCostAsset[i].value);
                        return false;
                    }
                }
                //扣除道具
                bagInfo.subGoods(refineCost);
                //扣除货币
                bagInfo.subGoods(refineCostAsset);
//                for (int i = 0; i<refineCostAsset.length;i++){
//                    bagInfo.addAsset(refineCostAsset[i].id, -refineCostAsset[i].value);
//                }
                //添加记录
                talentBean.setRefineLevel(talentBean.getRefineLevel() + 1);

                //添加已使用天赋点记录
                usedBean=  usedTalentNoteMap.get(type).get(tabType);
                usedBean.setUseCount(usedBean.getUseCount() + 1);
                return true;
            default:
                return false;
        }
    }


    /**
     * 天赋操作
     * @param tabType
     * @param talentId
     * @param isOnce
     * @return
     */
    public SGCommonProto.TalentObject.Builder onceUpdateTalent(SGCommonProto.E_TALENT_OPERATION tabType,int talentId,boolean isOnce){
        TalentConfig talentConfig = DataFactory.getInstance().getGameObject(DataFactory.TALENT_BEAN,talentId);
        //对天赋是否操作成功
        boolean isUpdateSuccess;
        if (isOnce) {
            //循环对天赋进行操作，直到失败为止
            boolean isUpdateTalent = updateTalent(tabType.getNumber(), talentConfig.type, talentId);
            isUpdateSuccess = isUpdateTalent;
            while (isUpdateTalent) {
                isUpdateTalent = updateTalent(tabType.getNumber(), talentConfig.type, talentId);
            }
        }else{
            isUpdateSuccess = updateTalent(tabType.getNumber(), talentConfig.type, talentId);
        }

        SGCommonProto.TalentObject.Builder c =SGCommonProto.TalentObject.newBuilder();
        TalentBean bean = getTalentBean(talentConfig.type,talentId);
        if ( !isUpdateSuccess){
            return null;
        }
        if (bean == null  ){
          return c;
        }
        else if (bean.getLevel() == 0 && bean.getRefineLevel() ==0){
            return c;
        }
        else {
            c.setTalentId(bean.getTalentId());
            c.setUpLevel(bean.getLevel());
            c.setRefineLevel(bean.getRefineLevel());
        }
        //getAllTalent();
        return c;
    }


    /**
     * 通过天赋类型和id获得天赋
     * @param type
     * @param talentId
     * @return
     */
    public TalentBean getTalentBean(int type,int talentId){
        //获得当前操作符文加点等级和精炼等级
        TalentBean talentBean = null;
        List<TalentBean> beanList = talentLibraryMap.get(type);
        if (beanList == null){
            LogUtils.error("================= +++++type="+type);
            return talentBean;
        }
        for (TalentBean bean:beanList){
            if (bean.getTalentId() == talentId){
                talentBean = bean;
                LogUtils.info("================= +++++talentId=" + talentId +"type ="+type);
                break;
            }
        }
        return talentBean;
    }


    /**
     * 通过天赋id获取玩家该天赋加点对象
     * @param talentId
     * @return
     */
    public TalentBean getPlayerTalent(int talentId){
        for (List<TalentBean> list:talentLibraryMap.values()){
            for (TalentBean bean:list){
                if (bean.getTalentId() == talentId){
                    return bean;
                }
            }
        }
         return null;
    }

    /**
     * 天赋列表
     * @return
     */
    public List<SGCommonProto.TalentObject.Builder> getAllTalent(){
        checkExp();
        List<SGCommonProto.TalentObject.Builder> objList  = new ArrayList<>();
        SGCommonProto.TalentObject.Builder c ;

        for (List<TalentBean> list:talentLibraryMap.values()){
            LogUtils.info("------------------size:"+list.size());
            for (TalentBean bean:list){
                LogUtils.info("------拉取天赋列表 值：id ="+bean.getTalentId() +"-----lv ="
                        +bean.getLevel()+"+++++++lv2:"+bean.getRefineLevel());
                if (bean.getLevel() == 0 && bean.getRefineLevel() == 0){
                    continue;
                }
                c = SGCommonProto.TalentObject.newBuilder();
                c.setTalentId(bean.getTalentId());
                c.setUpLevel(bean.getLevel());
                c.setRefineLevel(bean.getRefineLevel());
                objList.add(c);
            }
        }
        return objList;
    }

    /**
     * 效验经验值
     */
    public void checkExp(){
        List<TalentLevelConfig> list= DataFactory.getInstance().getDataList(DataFactory.TALENT_LEVEL);
        TalentLevelConfig config;
        if (!isTalentExp) {
            //累加经验值前做下矫正玩家经验
            config = DataFactory.getInstance().getGameObject(DataFactory.TALENT_LEVEL, talentLv);
            if (this.talentExp < config.upTalent) {
                this.talentExp = config.upTalent;
            }
            if (talentLv < list.size()) {
                config = DataFactory.getInstance().getGameObject(DataFactory.TALENT_LEVEL, talentLv + 1);
                if (config.upTalent <= this.talentExp) {
                    this.talentExp = config.upTalent - 1;
                }
            }
            isTalentExp =true;
        }
    }

    /**
     * 获取天赋对卡牌的属性加成
     * @param attrId 属性类型
     * @param armyId 兵种
     * @param campId 阵营
     * @param result 已有的加成
     * @return
     */
    public int getCardTalentAttribute(int attrId,int armyId,int campId,int result)
    {
        if (talentLibraryMap == null){
            LogUtils.info("talent is null");
            return 0;
        }
        //加成属性值
        int attrUpValue = 0;
        //精炼属性值
        int attrRefineValue = 0;
        TalentConfig  talentConfig;
        //有该属性加成的天赋集合
        List<TalentBean> haveBeanList = new ArrayList<>();
        //该天赋的加成阵营和兵种是否满足
        boolean isCamp = false;
        boolean isArmy = false;
        //遍历所有已学习的天赋获取加成的属性值
        for (List<TalentBean> talentBeanList:talentLibraryMap.values()){
            for (TalentBean bean:talentBeanList){
                //取得天赋加成配置
                List<TalentUpConfig> configList= DataFactory.getInstance().getTalentUpMap().get(bean.getTalentId());
                if (configList == null){
                    LogUtils.error("no get this talentConfig id="+bean.getTalentId());
                    continue;
                }
                talentConfig = DataFactory.getInstance().getGameObject(DataFactory.TALENT_BEAN,bean.getTalentId());
                for (int i = 0; i<talentConfig.camp.length;i++){
                    if (talentConfig.camp[i] ==campId) {
                        isCamp =true;
                        break;
                    }
                }
                for (int i = 0; i< talentConfig.army.length;i++){
                    if (talentConfig.army[i]== armyId){
                        isArmy =true;
                        break;
                    }
                }
                //判断天赋阵营和兵种
                if (isArmy && isCamp) {
                    for (TalentUpConfig config : configList) {
                        if (bean.getLevel() != config.level) {
                            continue;
                        }
                        //获取天赋对士兵加成的属性
                        for (int i = 0; i < config.attrId.length; i++) {
                            if (config.attrId[i] == attrId ) {
                                attrUpValue += config.getAddValue(result);
                                break;
                            }
                        }
                    }
                }
            }
        }
        for (TalentBean bean:haveBeanList){
            //取得天赋精炼配置
            List<TalentRefineConfig> configList= DataFactory.getInstance().getTalentRefineConfigMap().get(bean.getTalentId());
            if (configList == null){
                LogUtils.error("no get this TalentRefineConfig id="+bean.getTalentId());
                continue;
            }
            for (TalentRefineConfig config:configList){
                if (bean.getRefineLevel() == config.level){
                    attrRefineValue += config.getAddValue(attrUpValue);
                    break;
                }
            }
        }
        return attrUpValue + attrRefineValue;
    }

    /**
     * 天赋对主将的属性加成
     * @param attrId
     * @return
     */
    public int masterTalentAttribute(int attrId ){
        if (talentLibraryMap == null){
            LogUtils.info("talent is null");
            return 0;
        }
        MasterConfig masterConfig = DataFactory.getInstance().getGameObject(DataFactory.MASTER_KEY,
                player.master);
       int result =  masterConfig.getAttribute(attrId,player.getLv(),player.getMasterExtraAddAttributes(attrId));
        int attrUpValue  = 0 ;
        int  attrRefineValue = 0 ;
        //对该主将有该属性加成的天赋集合
        List<TalentBean> haveBeanList = new ArrayList<>();
        //遍历所有已学习的天赋获取加成的属性值
        TalentConfig talentConfig;
        for (List<TalentBean> talentBeanList:talentLibraryMap.values()) {
            for (TalentBean bean : talentBeanList) {
                //取得天赋加成配置
                List<TalentUpConfig> configList = DataFactory.getInstance().getTalentUpMap().get(bean.getTalentId());
                if (configList == null) {
                    LogUtils.error("no get this talentConfig id=" + bean.getTalentId());
                    continue;
                }
                talentConfig = DataFactory.getInstance().getGameObject(DataFactory.TALENT_BEAN, bean.getTalentId());
                if (talentConfig.master  == 1){
                    for (TalentUpConfig config : configList) {
                        if (bean.getLevel() != config.level) {
                            continue;
                        }
                        //获取天赋加成的属性
                        for (int i = 0; i < config.attrId.length; i++) {
                            if (config.attrId[i] == attrId ) {
                                attrUpValue += config.getAddValue(result);
                                break;
                            }
                        }
                    }
                }
            }
        }
        for (TalentBean bean:haveBeanList){
            //取得天赋精炼配置
            List<TalentRefineConfig> configList= DataFactory.getInstance().getTalentRefineConfigMap().get(bean.getTalentId());
            if (configList == null){
                LogUtils.error("no get this TalentRefineConfig id="+bean.getTalentId());
                continue;
            }
            for (TalentRefineConfig config:configList){
                if (bean.getRefineLevel() == config.level){
                    attrRefineValue += config.getAddValue(attrUpValue);
                    break;
                }
            }
        }
        return attrUpValue + attrRefineValue;
    }




    public Map<Integer, List<TalentBean>> getTalentLibraryMap() {
        return talentLibraryMap;
    }

    public void setTalentLibraryMap(Map<Integer, List<TalentBean>> talentLibraryMap) {
        this.talentLibraryMap = talentLibraryMap;
    }

    public Map<Integer, Map<Integer, TalentUsedBean>> getUsedTalentNoteMap() {
        return usedTalentNoteMap;
    }

    public void setUsedTalentNoteMap(Map<Integer, Map<Integer, TalentUsedBean>> usedTalentNoteMap) {
        this.usedTalentNoteMap = usedTalentNoteMap;
    }

    public int getTalentExp() {
        return talentExp;
    }

    public void setTalentExp(int talentExp) {
        this.talentExp = talentExp;
    }

    public int getTalentLv() {
        return talentLv;
    }

    public void setTalentLv(int talentLv) {
        this.talentLv = talentLv;
    }

    public int getTalentNote() {
        return talentNote;
    }

    public void setTalentNote(int talentNote) {
        this.talentNote = talentNote;
    }

}
